﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO.Ports;

namespace RefOffPDA
{
    /*
     * Class: GPS
     * 
     * Use: Used to connect to external GPS antenna, and record GPS locations
     * 
     * Author: Mike Skaife, mcs3@aber.ac.uk     * 
     */ 
    public class GPS
    {
        //Serial Port object
        static private System.IO.Ports.SerialPort m_serialPort1;
        
        //Holds String read from the GPS device
        private String m_readed="";
        
        //String indicating the name of the COM port used
        //to connect to the GPS antenna
        private String m_port;
        
        public GPS(String thePort)
        {
            m_port = thePort;
            m_serialPort1 = new System.IO.Ports.SerialPort(m_port);
        }

        //This method is used to read a GPS location from the connected antenna
        public String readGPS()
        {
            //boolean value indicating whether the reading from the
            //antenna is the correct GPS sentence type required
            bool gprmc = false;

            //Temporary string holding reading from antenna
            String location = "";

            //Loop to keep reading from device until sentence of the correct
            //type is found
            while (gprmc == false)
            {
                //Read a sentence from the antenna connected to the
                //specified COM port
                m_readed = m_serialPort1.ReadLine();

                //Check that the line read from the antenna is not blank
                if (m_readed.Length > 0)
                {
                    //Split the GPS sentence into separate 'words'
                    String[] words = m_readed.Split(',');
                    
                    //Check the 'Command Word' of the GPS sentence is as required
                    if (words[0].Equals("$GPRMC"))
                    {
                        //Store the GPS sentence
                        location = m_readed;

                        //Indicate that correct GPS location has been
                        //recorded to exit the loop
                        gprmc = true;
                    }
                }
            }

            //Return the GPS sentence recorded from the antenna
            return location;
        }

        //This method is used when creating a 'fake' GPS
        //location for a whistle event
        public String adjustGPS(String originalGPS)
        {
            //The original GPS location of the corresponding incident
            String origGPS = originalGPS;

            //String to hold the converted GPS sentence
            String adjustedGPS = "";
                      
            //String array used to hold separate 'words' of the original GPS sentence
            string[] Words = origGPS.Split(',');

            //Next the original GPS sentence has it's latitude split up into
            //degrees...
            String latDegrees = Words[3].Substring(0, 2);
            //minutes...
            String latMins = Words[3].Substring(2, 2);
            //and seconds
            int latSecsInt = Int32.Parse(Words[3].Substring(5, 4));

            //The value of the seconds is then adjusted slightly
            latSecsInt--;
            latSecsInt--;

            //A new latitude value can then be created by re-joining the separate parts
            String newLatitude = "" + latDegrees + latMins + "." + latSecsInt.ToString();
            Words[3] = newLatitude;
            
            //Create the new GPS sentence including the adjusted latitude value
            for (int i = 0; i < Words.Length; i++)
            {
                adjustedGPS += Words[i] + ",";
            }

            //Return the newly created GPS sentence
            return adjustedGPS;
        }

        //This method is used to open the specified COM port
        public void openPort()
        {
            m_serialPort1.Open();
        }

        //This method is used to close the specified COM port
        public void closePort()
        {
            m_serialPort1.Close();
        }
    }
}
